package Auth

import (
	"cmdb/base/Controller/Base"
	"cmdb/models"
	"github.com/astaxie/beego"
	"net/http"
	"strings"
)

type PrepareController struct {
	Base.MainController
	LoginUser *models.User
}

func (c *PrepareController) getNav() string {
	controllerName, _ := c.GetControllerAndAction() //获取Controller的名字
	return strings.ToLower(strings.TrimSuffix(controllerName, "Controller"))
}

func (c *PrepareController) Prepare() {
	//设置预设条件
	sessionKey := beego.AppConfig.DefaultString("auth::SessionKey", "user") //设置默认值
	sessionVal := c.GetSession(sessionKey)
	c.Data["loginUser"] = nil
	c.Data["nav"] = c.getNav()
	if sessionVal != nil {
		if pk, ok := sessionVal.(int); ok {
			if user := models.IdGetUser(pk); user != nil {
				c.LoginUser = user
				return
			}
		}
	}
	loginAction := beego.AppConfig.DefaultString("auth::LoginAction", "AuthController.Login") //设置默认值
	c.Redirect(beego.URLFor(loginAction), http.StatusFound)
}
